package zuo.algo.c19;

import zuo.algo.util.SortUtils;

import java.util.Arrays;

/**
 * 00:03-滑动窗口
 * 给定一个有序数组arr，代表数轴上从左到右n个点，给定一个正数L，代表一根长度为L的绳子
 * 求绳子最多能覆盖几个点？
 */
public class A01 {

    public static int exc1(int[] arr, int len) {
        int res = 0;
        for (int i = 0; i < arr.length; i++) {
            int count = 1;
            int begin = arr[i];
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[j] - begin > len) {
                    break;
                }
                count++;
            }
            res = Math.max(res, count);
        }
        return res;
    }

    public static int exc2(int[] arr, int len) {
        int res = 0;
        int left = 0, right = 0;
        int count = 0;
        while (right < arr.length) {
            if (arr[right] - arr[left] <= len) {
                count++;
                right++;
            } else {
                res = Math.max(res, count);
                left++;
                count--;
            }
        }
        res = Math.max(res, count);
        return res;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 100000; i++) {
            int[] arr = SortUtils.getRandomArr(10000, 10000);
            Arrays.sort(arr);
            int len = 50;

            int res1 = exc1(arr, len);
            int res2 = exc2(arr, len);
            if (res1 != res2) {
                SortUtils.printArr(arr);
                System.out.println(res1 + "-" + res2);
                break;
            }
            System.out.println(i);
        }
        System.out.println("over");
    }
}